Skin:
[NORMAL]
[BLUE] [DOS] [LIGHT]  / コピーするための表示 / 実行
このファイル: /home/web6047/www/cgi-bin/prj/20180707-SVC(Side View Character)/techtree/[E] batch/再帰を平で行う/javascript - stack使用の例 - ループを抜けて戻る例 - snap 20180715.html
1 <!DOCTYPE html>
2 <head>
3 <meta content="text/html; charset=UTF-8" http-equiv="content-type">
4 <title>再帰を平で行う</title>
5 <script>
6 /*
7 再帰を平で行う
8 */
9
10 console.clear();
11 console.log( "=============== script ==============" );
12 function $( id ) { return document.getElementById( id ); }
13 var HereDocument = /\/\*\s*([^]*?)\s*\*\//;
14 // usage: var txt = ( function() { /*multiTXT*/ } ).toString().match( HereDocument )[ 1 ];
15
16 var root = {
17 name : "1",
18 children : [
19 {
20 name : "1-1",
21 children : [
22 {
23 name : "1-1-1",
24 delay : true,
25 children : [
26 {
27 name : "1-1-1-1",
28 children : [
29
30 ]
31 },
32
33 ]
34 },
35 {
36 name : "1-1-2",
37 children : [
38
39 ]
40 }
41 ]
42 },
43 {
44 name : "1-2",
45 children : [
46 {
47 name : "1-2-1",
48 children : [
49
50 ]
51 },
52 {
53 name : "1-2-2",
54 children : [
55
56 ]
57 }
58 ]
59 }
60 ]
61 };
62
63 function onloadx() {
64 explorer1( root );
65 console.log( "***" );
66 explorer2( root );
67 }
68
69 function explorer1( object ) {
70 console.log( object.name );
71 for( var i = 0; i < object.children.length; i++ ) {
72 var child = object.children[ i ];
73 explorer1( child );
74 }
75 }
76
77 function explorer2( object ) {
78 /*
79 */
80 var envs = new Array(); //スタック
81 var env = {
82 object : object,
83 counter : 0
84 };
85
86 var func = function() {
87 loop1: while( 1 ) {
88
89 //check. ループを抜ける
90 if( env.object.delay ) {
91 console.log( "delay" );
92 env.object.delay = false;
93 setTimeout( func, 1000 ); //ループへ戻る
94 break;
95 }
96
97 //処理実行
98 console.log( env.object.name );
99
100 //check. 子の走査が終了した?
101 while( env.counter == env.object.children.length ) {
102 //check. 最上位である?
103 if( envs.length == 0 ) break loop1;
104
105 //親へ戻る
106 env = envs.pop();
107 }
108
109 //次の子へ移る
110 envs.push( env );
111 env = {
112 object : env.object.children[ env.counter++ ],
113 counter : 0
114 }
115
116 }//loop1:while
117
118 };//func()
119
120 func();
121 }
122 </script>
123 <style>
124 </style>
125 </head>
126 <body onload="onloadx();" style="">
127 <pre>
128 下記の方法で JavaScript コンソールを開いてページ更新してください。
129
130 JavaScript コンソールを開くには:
131 たいていのブラウザは … CTRL+SHIFT+J を押す。
132 InternetExplorer の場合は … F12 を押してから CTRL+2 を押す。
133
134 <a href="javascript:location.reload();">ページ更新</a>
135 </pre>
136 </body>
137 </html>